package com.xiaochong.tir.api.service;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.api.component.HbaseBaseQueryComponent;
import com.xiaochong.tir.common.data.hbasepo.CmcCurrencyInfo;
import com.xiaochong.tir.common.data.mapper.CoinSummarizeAffiliatedMapper;
import com.xiaochong.tir.common.data.mapper.CoinSummarizeMapper;
import com.xiaochong.tir.common.data.mapper.CurrencyInfoHbaseMapper;
import com.xiaochong.tir.common.data.mapper.CurrencyInfoMapper;
import com.xiaochong.tir.common.data.po.CoinSummarizeAffiliated;
import com.xiaochong.tir.common.data.po.CurrencyInfoHbase;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.ProjectOverviewVo;
import com.xiaochong.tir.common.util.ApiResultCodeUtils;
import com.xiaochong.tir.common.util.HbaseTableUtils;
import com.xiaochong.tir.common.util.OssImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Project : xc-tir
 * @Package Name : com.xiaochong.tir.api.service
 * @Description : TODO
 * @Author : daizhiqing@xiaochong.com
 * @Creation Date : 2018年07月05日下午8:07
 * @ModificationHistory Who When What
 */
@Service
public class CoinSummerizeService {

    @Autowired
    CoinSummarizeMapper coinSummarizeMapper;

    @Autowired
    CurrencyInfoMapper currencyInfoMapper;

    @Autowired
    CurrencyInfoHbaseMapper currencyInfoHbaseMapper;

    @Autowired
    private HbaseBaseQueryComponent hbaseBaseQueryComponent;

    @Autowired
    CoinSummarizeAffiliatedMapper coinSummarizeAffiliatedMapper;

    @Autowired
    OssImageUtil ossImageUtil;

    public ApiResultVo<ProjectOverviewVo> getProjectOverviewByCmcId(String cmcId) {
        ApiResultVo resultVo=new ApiResultVo();
        resultVo.setTradeId(LogTrace.getTrace().getTraceId());

        try {

            Map<String,Object> hashMap = currencyInfoMapper.getCurrencyInfoDetail(cmcId);
            if(hashMap != null){
                ProjectOverviewVo projectOverviewVo = JSON.parseObject(JSON.toJSONString(hashMap) , ProjectOverviewVo.class);


                List<CoinSummarizeAffiliated> list = coinSummarizeAffiliatedMapper.selectBySummarizeId(projectOverviewVo.getCoinSummarizeId());
                projectOverviewVo.setZeAffiliate(list);
                CurrencyInfoHbase currencyInfoHbase = currencyInfoHbaseMapper.getLastOneByCmcId(projectOverviewVo.getCmcId());
                if(currencyInfoHbase != null){
                    Map<String , Object> currencyInfoMap = hbaseBaseQueryComponent.selectData(HbaseTableUtils.CURRENCY_INFO_TABLE_NAME , currencyInfoHbase.getRowKey());
                    if(currencyInfoMap != null && currencyInfoMap.size() > 0){

                        CmcCurrencyInfo cmcCurrencyInfo = JSON.parseObject(JSON.toJSONString(currencyInfoMap), CmcCurrencyInfo.class);

                        projectOverviewVo.setMarketCap(new BigDecimal(cmcCurrencyInfo.getMaxSupply()!=null?cmcCurrencyInfo.getMaxSupply():"0")
                                .multiply(new BigDecimal(cmcCurrencyInfo.getPriceCny())).toString());
                        projectOverviewVo.setCirculatingSupply(cmcCurrencyInfo.getAvailableSupply());
                        projectOverviewVo.setCirculatingMarketCap(new BigDecimal(cmcCurrencyInfo.getAvailableSupply())
                                .multiply(new BigDecimal(cmcCurrencyInfo.getPriceCny())).toString());
                    }
                }
                projectOverviewVo.setImageUrl(ossImageUtil.fmtImg(projectOverviewVo.getImageUrl()));
                resultVo.setCode(ApiResultCodeUtils.BASE_SUCCESS.getCode());
                resultVo.setMessage(ApiResultCodeUtils.BASE_SUCCESS.getMsg());
                resultVo.setData(projectOverviewVo);
            }
            else{
                resultVo.setCode(ApiResultCodeUtils.SYSTEM_NULL.getCode());
                resultVo.setMessage(ApiResultCodeUtils.SYSTEM_NULL.getMsg());
                return resultVo;
            }
            return resultVo;
        }catch (Exception e){
            resultVo.setCode(ApiResultCodeUtils.SYSTEM_ERROR.getCode());
            resultVo.setMessage(ApiResultCodeUtils.SYSTEM_ERROR.getMsg());
            LogTrace.error(e.getMessage() , e);
        }

        return resultVo;
    }
}
